﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SolidWorks.Interop.sldworks;

namespace SolidworksDLL
{
    public class DrawingsHelp
    {
        public static List<Sheet> GetSheets(DrawingDoc drawingDoc)
        {
            string[] sheetNames = drawingDoc.GetSheetNames();
            List<Sheet> sheets = new List<Sheet>();

            foreach (var item in sheetNames)
            {
                Sheet sheet = drawingDoc.Sheet[item];
                sheets.Add(sheet);
            }

            return sheets;
        }

        public static Sheet GetSheets(DrawingDoc drawingDoc, string sheetName = "")
        {
            if (sheetName == "")
            {
                return drawingDoc.GetCurrentSheet();
            }
            else
            {
                return drawingDoc.Sheet[sheetName];
            }
        }

        public static List<View> GetSheetViews(DrawingDoc drawingDoc, string sheetName = "")
        {
            List<View> views = new List<View>();
            View view = null;
            if (sheetName == "")
            {
                view = drawingDoc.GetFirstView();
            }
            else
            {
                drawingDoc.ActivateSheet(sheetName);
                view = drawingDoc.GetFirstView();
            }

            while (view != null)
            {
                views.Add(view);
                view = view.GetNextView();
            }

            return views;
        }

        public static View GetSheetView(DrawingDoc drawingDoc, string viewName)
        {
            if (viewName == "")
            {
                return null;
            }

            List<View> views = GetSheetViews(drawingDoc);
            var view = views.FirstOrDefault(t => t.Name == viewName);
            return view;
        }

        /// <summary>
        /// GetTableAnnotations方法仅用于C++插件
        /// </summary>
        /// <param name="drawingDoc"></param>
        /// <param name="viewName"></param>
        /// <returns></returns>
        public static TableAnnotation GetViewBom(DrawingDoc drawingDoc, string viewName)
        {
            if (viewName == "")
            {
                return null;
            }

            var view = GetSheetView(drawingDoc, viewName);
            if (view == null)
            {
                return null;
            }
            //object[] tableAnnotations = (object[])view.GetTableAnnotations();
            ///GetTableAnnotations方法仅用于C++插件
            //if (tableAnnotations == null || tableAnnotations.Length == 0)
            //{
            //    return null;
            //}
            //TableAnnotation[] tableAnnotation1 = (TableAnnotation[])tableAnnotations;

            //foreach (TableAnnotation item in tableAnnotation)
            //{
            //    if (item.Type == 2)
            //    {
            //        return item;
            //    }
            //}
            //return tableAnnotation1.FirstOrDefault(t => t.Type == 2);

            TableAnnotation tableAnnotation = view.GetFirstTableAnnotation();
            while (tableAnnotation != null)
            {
                if (tableAnnotation.Type == 2)
                {
                    return tableAnnotation;
                }

                tableAnnotation = tableAnnotation.GetNext();
            }
            return null;
        }

        public static List<List<string>> GetViewBomData(TableAnnotation tableAnnotation)
        {
            List<List<string>> bomdata = new List<List<string>>();
            int r = tableAnnotation.RowCount;
            int c = tableAnnotation.ColumnCount;

            List<string> row1 = new List<string>();
            for (int j = 0; j < c; j++)
            {
                string value = tableAnnotation.GetColumnTitle(j);
                string[] values = value.Split('>');

                row1.Add(values[values.Length - 1]);
            }
            bomdata.Add(row1);

            for (int i = r - 2; i >= 0; i--)
            {
                List<string> row = new List<string>();
                for (int j = 0; j < c; j++)
                {
                    string value = tableAnnotation.DisplayedText2[i, j, false];
                    row.Add(value);
                }
                bomdata.Add(row);
            }

            return bomdata;
        }

        public static List<DisplayDimension> GetViewsDim(View view)
        {
            object[] displayDims = (object[])view.GetDisplayDimensions();
            if (displayDims == null || displayDims.Length == 0)
            {
                return null;
            }
            List<DisplayDimension> dims = new List<DisplayDimension>();
            foreach (DisplayDimension item in displayDims)
            {
                dims.Add(item);
            }

            return dims;
        }
    }
}